﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EpcTagLibrary.Pat
{
    public class SSCC : Pattern
    {
        public SSCC(PatternField[] fields, Type type)
            : base(fields, type)
        {
        }

        public override int GetTagBitLength()
        {
            return 96;
        }

        public override string GetScheme()
        {
            return "sscc-96";
        }

        public override string GetSchemeName()
        {
            return "sscc";
        }

        public PatternField FilterComponent
        {
            get
            {
                return Fields[0];
            }
        }

        public PatternField CompanyPrefixComponent
        {
            get
            {
                return Fields[1];
            }
        }

        public PatternField SerialReferenceComponent
        {
            get
            {
                return Fields[2];
            }
        }

        public class Factory : PatternFactory
        {

            public override Pattern GetPattern(string patBody, bool isGroupingPattern, bool isPureid)
            {
                if (!patBody.StartsWith("sscc"))
                {
                    throw new PatternParseException("Specified URI does not match the sscc URI for patterns.");
                }

                string[] Fields = patBody.Substring(patBody.IndexOf(":") + 1).Split(new char[1] { '.' });

                if (Fields.Length != 3)
                {
                    throw new PatternParseException("Specified URI does not match the sscc URI for patterns.");
                }

                PatternField[] PatFields = new PatternField[3];
                try
                {
                    PatFields[0] = new PatternField(Fields[0], isGroupingPattern, 3);
                    PatFields[0].Property = typeof(EpcTagLibrary.SSCC).GetProperty("Filter");

                    PatFields[1] = new PaddedPatternField(Fields[1], isGroupingPattern, 40);
                    PatFields[1].Property = typeof(EpcTagLibrary.SSCC).GetProperty("CompanyPrefix");

                    PatFields[2] = new PaddedPatternField(Fields[2], isGroupingPattern, 38);
                    PatFields[2].Property = typeof(EpcTagLibrary.SSCC).GetProperty("SerialReference");
                }
                catch (Exception e)
                {
                    throw new PatternParseException("Error occured setting up pattern component for a field: " + patBody, e);
                }

                SSCC Result = new SSCC(PatFields, typeof(EpcTagLibrary.SSCC));

                return Result;
            }
        }
    }
}
